<?php
namespace app\admin\controller;

use think\Controller;
use think\Session;
use think\Db;

class Login extends Controller
{
	//登陆时判断是否已经登陆
	public function _initialize()
    {
        if(Session::has('Adminname')&&Session::has('AdminId')&&Session::has('Adminpower')&&Session::has('AdminCompany'))
        {
            if(Session::get('Adminpower')==1){
                $this->success('您已登录!','../admin/index');
            }else{
                $this->success('您已登录!','../admin/jobs');
            }
        }
    }

    public function index()
    {
        return $this->fetch('login');
    }

    public function userlogin()
    {
    	$user = model('User');
    	$param = request()->post();
    	$param['ip'] = request()->ip();
    	$data = $user->userlogin($param);
    	$msg = json_decode($data,true);
		if($msg['code']!=400){
	        Session::set('Adminname',$msg['message']['rename']);
	        Session::set('AdminId',$msg['message']['id']);
            Session::set('Adminpower',$msg['message']['structure_id']);
	        Session::set('AdminCompany',$msg['message']['company']);
            if($msg['message']['structure_id']==1){
	           return json(['message'=>'超级管理员登录成功!'],200); 
            }else{
               return json(['message'=>'管理账号登录成功!'],200); 
            }
		}
		return json(['message'=>$msg['message']],200);
    }

    public function createuser()
    {
    	$user = model('User');
    	$param = request()->param();
    	$data = $user->createuser($param);
    	if($data==='phonealr'){
    		return json(['message'=>'已经存在的用户名']);
    	}else if($data==='emailalr'){
    		return json(['message'=>'该组织已经注册过账号']);
    	}else if($data){
    		return json(['message'=>'創建成功!']);
    	}else{
    		return json(['message'=>$user->getError()]);
    	}
    }
}
